from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext as _

from forms import AvatarForm

from djangohelper.helper import request_get_next

from simpleavatar.models import Photo
  #date = db.DateTimeProperty(auto_now_add=True)

from google.appengine.api import images
    
from google.appengine.api import memcache
from google.appengine.ext import db

@login_required
def change(request, extra_context={}, next=None):
    if request.method == "POST":
        user=request.user
        a=None
        try:
            a=request.FILES['avatar']
        except:
            #upload with no picture
            avatar_form = AvatarForm(user=request.user)
            return render_to_response('avatar/change.html',
                                      extra_context,
                                      context_instance = RequestContext(
                                                                        request,
                                                                        { 'avatar_form': avatar_form,
                                                                         'next': request_get_next(request) or next,
                                                                         'view_user':request.user }    #l78z to full fill the need of profile/base.html
                                                                        )
                                      )
        ss=a.read()
        a.close()
        u_img=images.resize(ss,80,80)
        pic_blob=db.Blob(u_img)
        image_key='p_%s'%user.key()
        gr=Photo(key_name=image_key,avatar=pic_blob)
        #gr.avatar=db.Blob(ss)
        #gr.user_key=str(user.key())
        gr.save()
        #memcache.add(image_key,u_img,60)
        memcache.delete(image_key)
        #print "dd"
        avatar_form = AvatarForm(user=request.user)
        return render_to_response(
        'avatar/change.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar_form': avatar_form,
              'next': request_get_next(request) or next,
              'view_user':request.user }    #l78z to full fill the need of profile/base.html
        )
    )
    avatar_form = AvatarForm(user=request.user)
    """
    if request.method == "POST":
        avatar_form = AvatarForm(request.POST, request.FILES, user=request.user)
        if  avatar_form.is_valid():
            avatar_form.save()
            request.user.message_set.create(
                message=_("Successfully updated your avatar."))
        return HttpResponseRedirect(request_get_next(request) or next)
    """
    return render_to_response(
        'avatar/change.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar_form': avatar_form,
              'next': request_get_next(request) or next,
              'view_user':request.user }    #l78z to full fill the need of profile/base.html
        )
    )
#change = login_required(change)
